package com.tiger.cloud.util.weChat;

import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class AccessToken {
    private final static String GRANT_TYPE = "client_credential";

    //微信测试号
//    private final static String APPID = "wx8fd3b10de66352bc";
//    private final static String SECRET = "2cb1e583d47281d0c6cf121a2f3486aa";
    //微信正式公众号
    private final static String APPID = "wx7e26d041b55f6ae5";
    private final static String SECRET = "cd9ae1eb71a814ecb373a19ad7b3297d";
    private final static String GRANT_TYPE_GETOPENID = "authorization_code";

    public  String getAccessToken() throws Exception{
        String accessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?" +
                                 "grant_type="+GRANT_TYPE+"&appid="+APPID+"&secret="+SECRET+"";
        String accessToken = "";
        String message = connection(accessTokenUrl);
        if(StringUtils.isEmpty(message)){
            return accessToken;
        }
        JSONObject.fromObject(message);
        Map map = (Map)JSONObject.fromObject(message);
        if(map.get("access_token")== null){
            return accessToken;
        }
        accessToken = map.get("access_token").toString();
        return accessToken;

    }
    public static String getTicket() throws Exception {
        String ticket = "";
        String access_token = new AccessToken().getAccessToken();
        String getJsApiTicketUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+access_token+"&type=jsapi";

        String message = connection(getJsApiTicketUrl);
        if(StringUtils.isEmpty(message)){
            return ticket;
        }
        JSONObject.fromObject(message);
        Map map = (Map)JSONObject.fromObject(message);

        if(map.get("ticket")!= null){
           return map.get("ticket").toString();
        }
       return ticket;
    }
    public static String getOpenid(String code) throws Exception {
        String openid = "";
        String accessTokenUrl  = "https://api.weixin.qq.com/sns/oauth2/access_token?" +
                                 "appid="+APPID+"&" +
                                 "secret="+SECRET+"" +
                                 "&code="+code+"" +
                                 "&grant_type="+GRANT_TYPE_GETOPENID+"";
        String message = connection(accessTokenUrl);
        if(StringUtils.isEmpty(message)){
              return openid;
        }
        Map map = (Map)JSONObject.fromObject(message);
        if(null == map.get("openid")){
            return openid;
        }
        return  map.get("openid").toString();

    }
    private static String connection(String connectionUrl) throws Exception {
        String message = "";
        try {
            URL url = new URL(connectionUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.connect();
            //获取返回的字符
            InputStream inputStream = connection.getInputStream();
            int size =inputStream.available();
            byte[] bs =new byte[size];
            inputStream.read(bs);
            message = new String(bs,"UTF-8");
        }catch (IOException e){
           throw  new Exception("连接服务器出错!"+e.getMessage().toLowerCase());
        }
        return message;
    }

    public static void main(String[] args) throws Exception {
       String access_token = new AccessToken().getAccessToken();
        System.out.println(access_token);
    }
}
